﻿using Enums;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace TemplateApiCore
{
    /// <summary>
    /// Swagger扩展类
    /// </summary>
    public static class SwaggerExtension
    {
        /// <summary>
        /// 添加Swagger扩展
        /// </summary>
        /// <param name="services"></param>
        /// <param name="controllerXmlPath"></param>
        public static void AddSwaggerExtension(this IServiceCollection services, string controllerXmlPath, string reqResXmlPath)
        {
            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen(options =>
            {
                #region Swagger显示注释
                options.IncludeXmlComments(controllerXmlPath);
                options.IncludeXmlComments(reqResXmlPath);
                #endregion

                #region Swagger版本控制
                //下面app.UseSwaggerUI()也需做出相应改变
                //多版本Swagger还需在控制器上明确指定当前版本[ApiExplorerSettings(GroupName = nameof(ApiVersionInfo.v1))]
                foreach (var item in Enum.GetValues(typeof(ApiVersionInfo)))
                    options.SwaggerDoc(item.ToString(), new OpenApiInfo { Title = "系统权限接口文档", Version = item.ToString() });
                #endregion

                #region Swagger传递Token
                //添加安全定义
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",
                    //参数添加在头部
                    //In = ParameterLocation.Header,//type为Http时可不写
                    //使用Authorize头部
                    Type = SecuritySchemeType.Http,
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });
                //添加安全要求
                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        },
                        new string[] { }
                    }
                });
                #endregion
            });
        }

        /// <summary>
        /// 启用Swagger
        /// </summary>
        /// <param name="app"></param>
        public static void UseSwaggerExtension(this WebApplication app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                #region Swagger使用各版本控制
                foreach (var item in Enum.GetValues(typeof(ApiVersionInfo)))
                    options.SwaggerEndpoint($"/swagger/{item}/swagger.json", $"系统权限{item}接口文档");
                #endregion
            });
        }
    }
}
